<!-- 车辆列表 -->
<template>
  <div class="dashboard-container vehicle customer-list-box">
    <div style="padding: 20px;">
      <el-card>
        <el-form
          ref="form"
          :model="queryParams"
          :inline="true"
          style="padding-bottom:-20px;display:flex"
          class="demo-form-inline"
        >
          <el-form-item label="车辆类型">
            <el-select
              v-model="queryParams.truckTypeId"
              placeholder="请选择"
            >
              <el-option
                v-for="item in form.CarTypeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="车牌号码">
            <el-input
              v-model="queryParams.licensePlate"
              placeholder="请输入车牌号码"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="searchList">搜索</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
        <el-tabs
          v-model="activeTab"
          type="border-card"
          style="margin-top: 20px"
          @tab-click="tabClick"
        >
          <!-- 全部 -->
          <el-tab-pane
            :label="'全部'"
            name="allData"
          > </el-tab-pane>
          <!-- 可用 -->
          <el-tab-pane
            :label="'可用'"
            name="availableData"
          > </el-tab-pane>
          <!-- 停用 -->
          <el-tab-pane
            :label="'停用'"
            name="deactivateData"
          > </el-tab-pane>
          <el-button
            type="danger"
            style="margin:20px 0"
            @click="showDialog"
          >新增车辆</el-button>
          <template>
            <el-table
              style="width: 100%"
              :data="truckListData"
            >
              <el-table-column
                label="序号"
                width="100px"
                type="index"
              >
              </el-table-column>
              <el-table-column
                label="车牌号码"
                prop="licensePlate"
              >
              </el-table-column>
              <el-table-column
                label="车辆类型"
                prop="truckTypeName"
              >
              </el-table-column>
              <el-table-column
                label="司机数量"
                prop="driverNum"
              >
              </el-table-column>
              <el-table-column label="车辆状态">
                <template v-slot="{ row }">
                  <span>
                    <i
                      style="width: 6px;
                            height: 6px;
                            background: #1dc779;
                            border-radius: 50%;
                            display:inline-block;
                            margin-right:6px;"
                      :style="{
                        background: row.workStatus == 0 ? 'red' : '#1dc779'
                      }"
                    ></i>{{ row.workStatus == 0 ? '停用' : '可用' }}
                  </span>
                </template>
              </el-table-column>
              <el-table-column
                label="GPS设备ID"
                prop="deviceGpsId"
              >
              </el-table-column>
              <el-table-column
                label="实载重量(T)"
                prop="allowableLoad"
              >
              </el-table-column>
              <el-table-column
                label="实载体积(方)"
                prop="allowableVolume"
              >
              </el-table-column>
              <el-table-column
                label="操作"
                align="center"
              >
                <template v-slot="{ row }">
                  <el-button
                    type="text"
                    size="small"
                    @click="$router.push(`vehicle-detail/?id=${row.id}`)"
                  >查看详情</el-button>
                  <el-button
                    type="text"
                    size="small"
                    :style="{ color: row.workStatus == 0 ? '' : 'red' }"
                    @click="open(row)"
                  >{{ row.workStatus == 0 ? '启用' : '停用' }}</el-button>
                  <el-button
                    type="text"
                    size="small"
                    @click="openText(row)"
                  >配置司机</el-button>
                </template>
              </el-table-column>
            </el-table>

            <div class="pagination">
              <el-pagination
                style="margin-bottom: 40px; margin-top:10px"
                :current-page="queryParams.page"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="queryParams.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              >
              </el-pagination>
            </div>
          </template>
        </el-tabs>
      </el-card>

      <!-- 新增 -->
      <vehicleAdd
        :dialog-visible.sync="dialogVisible"
        :car-type-list="form.CarTypeList"
        @updateTruck="truckList"
      ></vehicleAdd>
      <arrangeDriver
        ref="getDriverListByCar"
        :title-info="titleInfo"
        :form-base-ad="formBaseAd"
        :car-modal-info="carModalInfo"
        :drivers-list="driversList"
      ></arrangeDriver>
    </div>
  </div>
</template>
<script>
import vehicleAdd from '@/views/transit/components/vehicle-add.vue'
import arrangeDriver from '@/views/transit/components/arrange-driver.vue'
import {
  truckList,
  getCarTypeList,
  truckStatusUse,
  truckStatusNoUse
} from '@/api/transit'
export default {
  name: 'Vehicle',
  components: {
    vehicleAdd,
    arrangeDriver
  },
  data() {
    return {
      truckListData: [], // 全部表单数据
      // 分页器数据
      total: 0, // 总数据数
      queryParams: {
        page: 1, // 当前页数
        pageSize: 10, // 每页显示数量
        workStatus: '',
        truckTypeId: '', // 下拉列表选中值
        licensePlate: '' // 输入框值
      },
      form: {
        CarTypeList: [] // 下拉列表
      },
      // 控制子组件显示与隐藏的标识，类型为Boolean
      dialogVisible: false,
      // dialogFormVisible: false,
      // 子组件数据
      titleInfo: {},
      formBaseAd: {},
      carModalInfo: {},
      driversList: [],
      code: '',
      activeTab: 'allData'
    }
  },
  created() {
    this.truckList()
    this.getCarTypeList()
  },
  methods: {
    // 获取表单数据
    async truckList() {
      const { data } = await truckList(this.queryParams)
      this.total = +data.counts
      // console.log(data)
      // 总数
      this.truckListData = data.items
    },
    // 当前页数改变时
    handleCurrentChange(newpage) {
      this.queryParams.page = newpage
      this.truckList()
    },
    // 每页显示数量变化时
    handleSizeChange(newPageSize) {
      this.queryParams.pageSize = newPageSize
      this.truckList()
    },
    // 获取下拉列表数据
    async getCarTypeList() {
      const { data } = await getCarTypeList()
      this.form.CarTypeList = data
    },
    // 点击搜索
    async searchList() {
      this.truckList()
    },
    // 点击重置
    resetForm() {
      this.queryParams = {
        page: 1, // 当前页数
        pageSize: 10, // 每页显示数量
        truckTypeId: '', // 下拉列表选中值
        licensePlate: '' // 输入框值
      }
    },
    // 切换tab状态栏
    tabClick(val) {
      if (val.name === 'availableData') {
        this.queryParams.workStatus = '0'
        this.truckList()
      } else if (val.name === 'deactivateData') {
        this.queryParams.workStatus = '1'
        this.truckList()
      } else {
        this.queryParams.workStatus = ''
        this.truckList()
      }
    },
    open(row) {
      this.$confirm(
        `确定要${row.workStatus === 0 ? '启用' : '停用'}吗?车辆${
          row.workStatus === 0
            ? '启用需要满足以下条件：1.车辆信息已完善2.绑定司机>=2，且有排班'
            : '停用需要满足以下条件：1.当前无运输任务 2.车辆无关联运输线路！   注：停用后司机将自动解除绑定'
        }`,
        `车辆${row.workStatus === 0 ? '启用' : '停用'}`,
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }
      )
        .then(async () => {
          if (row.workStatus === 0) {
            const { code, msg } = await truckStatusUse(row.id)
            this.code = code
            if (this.code === 1) {
              return this.$message.error(msg)
            } else {
              this.truckList()
              return this.$message.success(msg)
            }
          } else {
            const { code, msg } = await truckStatusNoUse(row.id)
            this.code = code
            if (this.code === 1) {
              return this.$message.error(msg)
            } else {
              this.truckList()
              return this.$message.success(msg)
            }
          }
        })
        .catch(() => {
          this.$message({
            type: 'error',
            message: '已取消'
          })
        })
    },
    showDialog() {
      this.dialogVisible = true
    },
    // 配置司机
    openText(row) {
      this.$confirm(
        '配置司机需要满足以下条件： 1. 车辆信息已完善 2. 车辆无未完成运输任务',
        '车辆配置',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }
      )
        .then(async () => {
          // this.dialogFormVisible = true
          this.carModalInfo = row
          this.$refs.getDriverListByCar.dialogFormV()
          this.$nextTick(() => {
            this.$refs.getDriverListByCar.getDriverListByCar(row.id)
          })
          // console.log(row)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.alert {
  margin: 10px 0px;
}
.el-form-item {
  display: flex;
}
.pagination {
  margin-top: 10px;
}
.vehicle /deep/ .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

.vehicle {
  /deep/ .el-dialog__title {
    width: 73px;
    height: 25px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #20232a;
    line-height: 25px;
    letter-spacing: 0px;
  }
  /deep/ .el-dialog__body {
    text-align: center;
    padding: 20px 20px 0px 20px;
  }
  .warn-img {
    width: 40px;
  }
  p {
    height: 24px;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    color: #818693;
    line-height: 24px;
  }
}

// 下拉框
/deep/ .el-select {
  width: 100%;
}
</style>

<style>
.startconfirm {
  width: 481px;
}
.startconfirm .el-message-box__header {
  padding-bottom: 4px;
}
.startconfirm .el-message-box__container {
  margin-bottom: 15px;
}
.confirm .el-message-box__status.el-icon-warning {
  left: 70px;
}
.confirm.el-message-box__title {
  font-size: 16px;
}
.confirm .el-message-box__message p {
  color: #f92d2d;
  margin-top: 5px;
}
.confirm .el-message-box__btns {
  text-align: center;
  margin-top: 10px;
}
.pagination {
  padding-top: 10px;
  text-align: center;
}
</style>
